<?php
// +----------------------------------------------------------------------
// | 07FLYSAAS [基于ThinkPHP5.0开发]
// +----------------------------------------------------------------------
// | Copyright (c) 2025-2035 http://www.07fly.xyz
// +----------------------------------------------------------------------
// | Professional because of focus  Persevering because of happiness
// +----------------------------------------------------------------------
// | Author: 开发人生 <goodkfrs@qq.com>
// +----------------------------------------------------------------------

namespace app\plat\logic;

use think\Db;

/**
 * 系统公告=》逻辑层
 */
class TenNotify extends PlatBase
{
    /**
     * 系统公告列表
     * @param array $where
     * @param bool $field
     * @param string $order
     * @param int|mixed $paginate
     * @return
     */
    public function getTenNotifyList($where = [], $field = true, $order = '', $paginate = DB_LIST_ROWS)
    {
        $list = $this->modelTenNotify->getList($where, $field, $order, $paginate);
        foreach ($list as &$row) {
            $row['rece_type_text'] = ($row['rece_type'] == '1') ? '指定人员' : '全体人员';
            $row['create_user_name'] = $this->modelTenUser->getValue(['id' => $row['create_user_id']], 'realname');
        }
        return $list;
    }

    /**
     * 系统公告添加
     * @param array $data
     * @return array
     */
    public function tenNotifyAdd($data = [])
    {
        $validate_result = $this->validateTenNotify->scene('add')->check($data);
        if (!$validate_result) {
            return [RESULT_ERROR, $this->validateTenNotify->getError()];
        }

        $data['create_user_id'] = TEN_USER_ID;
        $result = $this->modelTenNotify->setInfo($data);

        //分发到每个帐号下面
        if ($data['rece_type'] == '1') {//近指人员
            $sys_user_arr = str2arr($data['rece_user_ids']);
        } else {
            //当前租户下面系统所人员
            $map['ten_tenant_id'] = TEN_TENANT_ID;
            $sys_user_arr = $this->modelTenTenantUser->getColumn($map, 'ten_user_id');
        }
        //分发到每个帐号下面
        $this->modelTenNotify->addNotifyUserList($result, $sys_user_arr);

        $url = url('show');
        $result && ten_log('新增', '新增系统公告：' . $data['name']);
        return $result ? [RESULT_SUCCESS, '添加成功', $url] : [RESULT_ERROR, $this->modelTenNotify->getError()];
    }

    /**
     * 系统公告编辑
     * @param array $data
     * @return array
     */
    public function tenNotifyEdit($data = [])
    {
        $validate_result = $this->validateTenNotify->scene('edit')->check($data);
        if (!$validate_result) {
            return [RESULT_ERROR, $this->validateTenNotify->getError()];
        }
        $url = url('show');
        $result = $this->modelTenNotify->setInfo($data);

        //分发到每个帐号下面
        if ($data['rece_type'] == '1') {//近指人员
            $sys_user_arr = str2arr($data['rece_user_id']);
        } else {
            //当前租户下面系统所人员
            $map['ten_tenant_id'] = TEN_TENANT_ID;
            $sys_user_arr = $this->modelTenTenantUser->getColumn($map, 'ten_user_id');
        }
        //分发到每个帐号下面
        $this->modelTenNotify->addNotifyUserList($data['id'], $sys_user_arr);

        $result && ten_log('编辑', '编辑系统公告，name：' . $data['name']);
        return $result ? [RESULT_SUCCESS, '编辑成功', $url] : [RESULT_ERROR, $this->modelTenNotify->getError()];
    }

    /**
     * 系统公告删除
     * @param array $where
     * @return array
     */
    public function tenNotifyDel($data = [])
    {
        if (!empty($data['id'])) {
            $where['id'] = ['in', $data['id']];
            $result = $this->modelTenNotify->deleteInfo($where, true);
            $where2['notify_id'] = ['in', $data['id']];
            $result && $this->modelTenNotifyUser->deleteInfo($where2, true);
            $result && ten_log('删除', '删除系统公告，where：' . http_build_query($where));
        }
        return $result ? [RESULT_SUCCESS, '删除成功'] : [RESULT_ERROR, $this->modelTenNotify->getError()];
    }

    /**
     * 系统公告信息
     * @param array $where
     * @param bool $field
     * @return
     */
    public function getTenNotifyInfo($where = [], $field = true)
    {
        $info = $this->modelTenNotify->getInfo($where, $field);
        $info['create_user_name'] = $this->modelTenUser->getValue(['id' => $info['create_user_id']], 'realname');
        return $info;
    }

    /**
     * 系统主面显示自己的公告
     * @param array $data
     * @return mixed
     * Author: 开发人生 goodkfrs@qq.com
     * Date: 2021/10/29 0029 12:08
     */
    public function getTenNotifyListMy($data = [])
    {
        $where = [];
        if ($data['owner_user_id']) {
            $where['u.owner_user_id'] = $data['owner_user_id'];
        }
        if (isset($data['read_state'])) {
            if (!empty($data['read_state']) || is_numeric($data['read_state'])) {
                $where['read_state'] = ['=', '' . $data['read_state'] . ''];
            }
        }
        $this->modelTenNotify->alias('a');
        $join = [
            [SYS_DB_PREFIX . 'sys_notify_user u', 'a.id = u.notify_id', 'RIGHT'],
        ];
        $this->modelTenNotify->join = $join;
        $list = $this->modelTenNotify->getList($where, 'a.name,a.create_user_id,a.id,a.create_time', 'u.create_time desc', false);
        foreach ($list as &$row) {
            $row['create_user_name'] = $this->modelTenUser->getValue(['id' => $row['create_user_id']], 'realname');
        }
        return $list;
    }

    public function getWhere($data = [])
    {
        $where = [];
        //关键字查
        !empty($data['keywords']) && $where['name|content'] = ['like', '%' . $data['keywords'] . '%'];
        //创建时间
        if (!empty($data['create_time'])) {
            $range_date = rangedate2arr($data['create_time'], '-', 'int');
            $where['create_time'] = ['between', [$range_date[0], $range_date[1]]];
        }
        return $where;
    }
}